package com.kylin.domain.merchant.aggregate;

import com.kylin.domain.merchant.command.CreateMerchantCommand;
import com.kylin.domain.merchant.event.MerchantCreatedEvent;
import com.kylin.shared.enums.YesNoEnum;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.spring.stereotype.Aggregate;
import java.time.LocalDateTime;

import static org.axonframework.modelling.command.AggregateLifecycle.apply;

@Slf4j
@Getter
@Setter(value = AccessLevel.PRIVATE)
@Aggregate
@NoArgsConstructor
public class MerchantAggregate {
    @AggregateIdentifier
    private Long id;
    private String username;
    private String password;
    private String salt;
    private String phone;
    private String email;
    private String qq;
    private String roles;
    private Integer state;
    private YesNoEnum removed;
    private Long createdBy;
    private Long updatedBy;
    private Long removedBy;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    private LocalDateTime removedAt;


    @CommandHandler
    public MerchantAggregate(CreateMerchantCommand command) {
        apply(MerchantCreatedEvent.builder()
                .id(command.getId())
                .username(command.getUsername())
                .password(command.getPassword())
                .salt(command.getSalt())
                .phone(command.getPhone())
                .email(command.getEmail())
                .qq(command.getQq())
                .roles(command.getRoles())
                .state(command.getState())
                .createdBy(command.getCreatedBy())
                .createdAt(LocalDateTime.now())
                .removed(YesNoEnum.N).build());
    }
}
